package com.txsj.scan.presenter;

import com.txsj.scan.base.BasePresenter;
import com.txsj.scan.bean.BaseListResult;
import com.txsj.scan.bean.SimpleBatchBean;
import com.txsj.scan.bean.RecordInfo;
import com.txsj.scan.bean.WarehouseBean;
import com.txsj.scan.http.ApiServer;
import com.txsj.scan.http.requestframework.RetrofitManager;
import com.txsj.scan.utils.PreferenceUtil;
import com.txsj.scan.view.IBatchView;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import io.reactivex.Observer;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.schedulers.Schedulers;

/**
 * @Created by WangChuangJun on 2018/11/21
 * @Describe
 */
public class SimpleBatchPresenter extends BasePresenter<IBatchView> {

    private WarehouseBean warehouseBean;
    private List<RecordInfo> recordInfoList;
    private long times_long = 1000*60*30;//时间差

    public SimpleBatchPresenter(IBatchView view) {
        super(view);
    }

    @Override
    protected void initModel() {
        recordInfoList = new ArrayList<>();
    }

    public void getBatchHttpData(long gs1_num, final int pageNo) {
        Map<String,String> params = new HashMap<>();
        StringBuilder builder = new StringBuilder();
        builder.append(PreferenceUtil.getInstance().getString(PreferenceUtil.BRAND_ID,""));
        builder.append("|");
        builder.append(PreferenceUtil.getInstance().getInt(PreferenceUtil.USER_ID,0));
        builder.append("|");
        builder.append(PreferenceUtil.getInstance().getString(PreferenceUtil.TOKEN,""));
        builder.append("|");
        builder.append(0);
        builder.append("|");
        params.put("brandtoken", builder.toString());
        params.put("gs1_num", gs1_num+"");
        params.put("page", pageNo+"");
        params.put("limit", "20");
        view.onLoading();
        RetrofitManager.getDefault().create(ApiServer.class).getBatch(RetrofitManager.getHeader(),params)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<BaseListResult<SimpleBatchBean>>() {
                    @Override
                    public void onSubscribe(Disposable d) {
                        compositeDisposable.add(d);
                    }

                    @Override
                    public void onNext(BaseListResult<SimpleBatchBean> result) {
                        if (result.isSuccess()){
                            if (null != result.getData() && result.getData().size() > 0){
                                List<SimpleBatchBean> list = result.getData();
                                view.onSuccess(list);
                            }else {
                                if (pageNo == 1) {
                                    view.onError("暂无数据");
                                }else {
                                    view.onLoadEnd("加载完毕");
                                }
                            }
                        }else {
                            if (pageNo == 1){
                                view.onError("加载失败");
                            }else {
                                view.onLoadMoreError("加载失败");
                            }
                        }
                    }

                    @Override
                    public void onError(Throwable e) {
                        if (pageNo == 1){
                            view.onError("加载失败");
                        }else {
                            view.onLoadMoreError("加载失败");
                        }
                    }

                    @Override
                    public void onComplete() {

                    }
                });
    }
}
